/*
MIT License

Copyright (c) 2022 guo-yichen@Easylosu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
SOFTWARE.

*/

#include<iostream>
#include<stdlib.h>
#include<cstring>
#include<cmath>
#include<random>
#include<ctime>
#include "libmath.hpp"
using namespace std;
extern "C"{
LNI losu_math_abs(LNI num) {
	losu_math_rt[0] = '\0';
	double number=atof(num);
	if(number<0) {
		for(int i=1; i<strlen(num); i++) {
			losu_math_rt[i-1]=num[i];
		}
	} else {
		for(int i=0; i<strlen(num); i++) {
			losu_math_rt[i]=num[i];
		}
	}
	return losu_math_rt;
}
LNI losu_math_log(LNI down,LNI up) {
	losu_math_rt[0] = '\0';
	double number=log(atof(up))/log(atof(down));
	if(number==(int)number)	sprintf(losu_math_rt,"%d",(int)number);
	else	sprintf(losu_math_rt,"%lf",number);
	return losu_math_rt;
}
LNI losu_math_pow(LNI down,LNI up) {
	losu_math_rt[0] = '\0';
	double number=pow(atof(down),atof(up));
	if(number==(int)number)	sprintf(losu_math_rt,"%d",(int)number);
	else	sprintf(losu_math_rt,"%lf",number);
	return losu_math_rt;
}
LNI losu_math_factorial(LNI num) {
	losu_math_rt[0] = '\0';
	long long sum=1;
	for(int i=1;i<=atoi(num);i++){
		sum*=i;
	}
	sprintf(losu_math_rt,"%lld",sum);
	return losu_math_rt;
}
LNI losu_math_rand(LNI Min,LNI Max) {
	losu_math_rt[0] = '\0';
	default_random_engine e(time(0));
	uniform_real_distribution<double> u(atof(Min),atof(Max));
	sprintf(losu_math_rt,"%lf",u(e));
	return losu_math_rt;	
}
LNI losu_math_randint(LNI Min,LNI Max) {
	losu_math_rt[0] = '\0';
	clock_t _t;
	_t = clock();
    srand(_t);  
	int ans=rand()%(atoi(Max)-atoi(Min)+1)+atoi(Min);
	sprintf(losu_math_rt,"%d",ans);
	return losu_math_rt;
}

}
